package com.gxar.quick.ar.server.base.model.protocol;

import com.gxar.common.entity.SystemCodeEnum;
import com.gxar.common.utils.AssertUtils;
import com.gxar.common.utils.StringSpliceUtils;
import com.gxar.common.utils.StringUtils;
import com.gxar.quick.ar.server.base.common.utils.NumberUtils;
import lombok.Data;

import java.util.Objects;

/**
 * AR颜色值
 *
 * @author zxh
 * @version 1.0
 * @created 2024/02/23.
 */
@Data
public class ArColor {

    private Double r;

    private Double g;

    private Double b;

    public ArColor() {

    }

    public ArColor(Double r, Double g, Double b) {
        this.r = r;
        this.g = g;
        this.b = b;
    }

    @Override
    public String toString() {
        return StringSpliceUtils.splice("{} {} {}",
                NumberUtils.formatDouble(r),
                NumberUtils.formatDouble(g),
                NumberUtils.formatDouble(b));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ArColor color = (ArColor) o;
        return Objects.equals(r, color.r) && Objects.equals(g, color.g) && Objects.equals(b, color.b);
    }

    @Override
    public int hashCode() {
        return Objects.hash(r, g, b);
    }

    public static ArColor create(Double r, Double g, Double b) {
        return new ArColor(r, g, b);
    }

    public static ArColor create(ArColor input) {
        AssertUtils.notNull(input, SystemCodeEnum.PARAM_ERROR.getRet(), "输入不能为空");
        return new ArColor(input.r, input.b, input.b);
    }

    public static ArColor from(String inputStr) {
        if (StringUtils.isEmpty(inputStr)) {
            return null;
        }

        String[] inputArrays = inputStr.split(" ");
        if (inputArrays.length != 3) {
            return null;
        }

        Double r = Double.parseDouble(inputArrays[0]);
        Double g = Double.parseDouble(inputArrays[1]);
        Double b = Double.parseDouble(inputArrays[2]);

        return new ArColor(r, g, b);
    }
}
